<?php

namespace App\Http\Controllers\Back;

use App\Http\Controllers\Controller;
use App\Model\ArticleCategories;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;


class ArticleCategoriesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $articleCategories = ArticleCategories::orderBy('sort','asc')->get();

        $data = [];
        foreach ($articleCategories as $articleCategory){
            $item = [
                'uuid' => $articleCategory->id,
                'title' => $articleCategory->title,
                'sort' => $articleCategory->sort,
                'is_enabled'  => $articleCategory->is_enabled,
                'articleNum'=> $articleCategory->article->count(),
                'created_at' => $articleCategory->created_at->format('Y-m-d H:i:s'),
                'updated_at' => $articleCategory->updated_at->format('Y-m-d H:i:s'),

            ];
            $data[] = $item;
        }

        return $this->renderJson($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if(\DB::select('select * from article_categories where title =? and deleted_at =?',[$request->input('title'), null]))
        {
            return $this->fail('分类名称重复！');
        }
        if(\DB::select('select * from article_categories where sort =? and deleted_at =?',[$request->input('sort'), null]))
        {
            return $this->fail('排序重复！');
        }
        $article_categories = new ArticleCategories();
        $article_categories->title = $request->input('title');
        $article_categories->sort  = $request->input('sort');
        $data =  $article_categories->save();
        if ($data == true){
            Cache::forget('categories');
            return $this->renderJson('成功');
        }
        return $this->error('失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $item = ArticleCategories::where('id', $id)->firstOrFail();


        return $this->renderJson($item);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

        $item = ArticleCategories::where('id', $id)->firstOrFail();
        $item->title = $request->input('title');
        $item->sort = $request->input('sort');
        $res =  $item->save();
        if ($res == true){
            Cache::forget('categories');
            return $this->success('修改成功！');
        }
        return $this->error('修改失败！');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $item = ArticleCategories::findOrFail($id);

        $res  =  $item->delete();
        if ($res == true){
            Cache::forget('categories');
            return $this->success('删除成功！');
        }
        return $this->error('删除失败！');

    }

    public function isEnable(Request $request)
    {

        $item = ArticleCategories::findOrFail($request->input('id'));
        $item->is_enabled = $request->input('is_enabled');
        $res = $item->save();
        if ($res == true){
            Cache::forget('categories');
            return $this->success('修改成功！');
        }
        return $this->error('修改失败！');
    }
}
